﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace mergeDocument
{
    public partial class Form1 : Form
    {
        string fileName1;
        string fileName2;
        string mergefilename="";
        string mergefile;
        string file1;
        string file2;
        string folder ;
        string mergeFile_Name;

        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void firstfilename_TextChanged(object sender, EventArgs e)
        {
            fileName1 = firstfilename.Text;
        }

        private void secondfilename_TextChanged(object sender, EventArgs e)
        {
            fileName2 = secondfilename.Text;
        }

        private void cffileB_Click(object sender, EventArgs e)
        {
            try
            {
                FileStream fs = new FileStream(fileName1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                using (StreamReader sr = new StreamReader(fs))
                {
                    file1 = sr.ReadToEnd();
                    sr.Close();
                }
                firstfilelabel.Text = "状态：第一个文件选择成功";

            }
            catch
            {
                firstfilelabel.Text = "状态：第一个文件选择失败，检查路径是否正确等";
            }

        }

        private void csfileB_Click(object sender, EventArgs e)
        {
            try
            {
                using (StreamReader sr = new StreamReader(fileName2, Encoding.UTF8))
                {
                    file2 = sr.ReadToEnd();
                    sr.Close();
                }
                secondfilelabel.Text = "状态：第二个文件选择成功";
            }
            catch
            {
                firstfilelabel.Text = "状态：第二个文件选择失败，检查路径是否正确等";
            }
        }

        private void mergeButton_Click(object sender, EventArgs e)
        {
            if(fileName1==""|fileName2==""|mergefilename == "")
            {
                mergeFileLabel.Text = "Error!用于合并的文件和合并生成文件文件名不能为空！";
            }
            else
            {
                try {
                    folder = System.Windows.Forms.Application.StartupPath + "\\Data\\";

                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                  mergeFile_Name = folder + mergefilename + ".txt";
                    if (File.Exists(mergeFile_Name))
                    {
                        FileStream fs = new FileStream(mergeFile_Name, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                        StreamWriter sr = new StreamWriter(fs,Encoding.UTF8);
                        sr.Write(file1);
                        sr.Write(file2);
                        sr.Close();
                        mergeFileLabel.Text = "合并成功！路径：" + mergeFile_Name.ToString();
                    }
                    else
                    {
                        FileStream fs = new FileStream(mergeFile_Name, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                        StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                        sw.Write(file1);
                        sw.Write(file2);
                        sw.Close();
                        mergeFileLabel.Text = "合并成功！路径：" + mergeFile_Name.ToString();
                    
                }
                }
                catch
                {
                    mergeFileLabel.Text = "合并失败！";
                }
            }
        }

        private void openMergeButton_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(mergeFile_Name);
        }

        private void mergefilenametext_TextChanged(object sender, EventArgs e)
        {
            mergefilename = mergefilenametext.Text;
        }
    }
}
